package com.android.jingweiapp.activity;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

import com.android.jingweiapp.R;
import com.android.jingweiapp.XMLPhase.GetVideoDetailXMLPhase;
import com.android.jingweiapp.bean.NewsBean;
import com.android.jingweiapp.util.Contacts;
import com.android.jingweiapp.util.HttpUtil;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * 视频新闻
 */
public class NewsDetailVideoActivity extends AppCompatActivity {

    @Bind(R.id.btn_back)
    Button btnBack;
    @Bind(R.id.tv_detail_title)
    TextView tvDetailTitle;
    @Bind(R.id.tv_detail_time)
    TextView tvDetailTime;
    @Bind(R.id.tv_detail_from)
    TextView tvDetailFrom;
    @Bind(R.id.video_view)
    WebView videoView;

    private ProgressDialog progressDialog;
    private String title;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info_detail_video);
        ButterKnife.bind(this);

        init();
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void init() {
        progressDialog = ProgressDialog.show(this, "加载中", "请稍后", true, false);
        Parcelable beans = getIntent().getParcelableExtra(Contacts.KEY.KEY_OBJECT);

        getNewsDetail((NewsBean) beans);

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        videoView.getSettings().setJavaScriptEnabled(true);
    }

    private void dealData(String str) {

        String html = "<html><head><script type=\"text/javascript\" charset=\"utf-8\" src=\"http://jackzhang1204.github.io/sewise/sewise_player//player/js/swfobject.js\"></script><style type=\"text/css\" media=\"screen\">#swf-container {visibility:hidden}</style></head><body><div style=\"width: 640px; height: 360px; \"><script type=\"text/javascript\" src=\"http://jackzhang1204.github.io/sewise/sewise_player//player/sewise.player.min.js?server=vod&amp;type=flv&amp;videourl=http://218.61.196.241/jxw/images/UploadImg/084146.flv&amp;sourceid=&amp;autostart=false&amp;starttime=0&amp;lang=en_US&amp;logo=&amp;title=特高压输电通道建设将给内蒙古带来什么  上&amp;buffer=5&amp;skin=vodWhite&amp;fallbackurls=%7B%0A%09%22ogg%22%3A%20%22http%3A%2F%2Fwww.w3schools.com%2Fhtml%2Fmov_bbb.ogg%22%2C%0A%09%22webm%22%3A%20%22http%3A%2F%2Fwww.w3schools.com%2Fhtml%2Fmov_bbb.webm%22%0A%7D\"></script></div></div></body></html>";
//        html = html.replaceAll("@title@", title);
//        html = html.replace("@url@", Contacts.BASE_PIC_URL + str);

        videoView.loadData(html, "text/html", "UTF-8");
        progressDialog.dismiss();
//        videoView.loadUrl(Contacts.BASE_PIC_URL + str);
    }

    private void getNewsDetail(NewsBean newsBean) {
        title = newsBean.getTitle();
        tvDetailTitle.setText(title);
        tvDetailFrom.setText(newsBean.getFrom());
        tvDetailTime.setText(newsBean.getTime());

        new NewsAsyncTask().execute(newsBean.getId());
    }

    class NewsAsyncTask extends AsyncTask<String, Integer, String> {

        @Override
        protected String doInBackground(String[] params) {
            return HttpUtil.postWithContent(Contacts.BASE_URL,
                    GetVideoDetailXMLPhase.createParam(NewsDetailVideoActivity.this,
                            params[0]),
                    new GetVideoDetailXMLPhase());
        }

        @Override
        protected void onPostExecute(String str) {
            super.onPostExecute(str);
            dealData(str);
        }
    }


}
